//
//  CMDBUserManageViewController.swift
//  CMDB
//
//  Created by 王磊 on 23/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBUserManageViewController: NormalTitleViewController {
    
    private var tableView: CMDBUserManageTableView = CMDBUserManageTableView.getInstance()
    
    private var emptyView: CMDBBuildUserManageView = CMDBBuildUserManageView.getInstance()
    
    override func setup() {
        super.setup()
        
        buildTableView()
        
        buildEmptyView()
        
    }
}
extension CMDBUserManageViewController {
    
    private func buildTableView() {
        
        tableView.addTarget(self, action: #selector(itemClick), forEvent: TSItemClickType.TableViewItem)
        
        contentView!.addSubview(tableView)
        
        tableView.frame = contentView!.bounds
        
        contentView!.backgroundColor = UIColor.Hex(0xE2E2E2)
        
        tableView.hidden = true
    }
    
    private func buildEmptyView() {
        
        emptyView.text = "您名下尚未生成二级用户"
        
        contentView!.addSubview(emptyView)
        
        emptyView.frame = contentView!.bounds
        
        emptyView.addTarget(self, action: #selector(buildTriggered), forEvent: TSItemClickType.ButtonItem)
    }
    
    @objc private func buildTriggered(argu: TSArguInfo) {
        
        let modifyUserVC = CMDBModifyUserManageViewController()
        
        modifyUserVC.title = "二级用户信息修改"
        
        navigationController!.pushViewController(modifyUserVC, animated: true)
        
        modifyUserVC.dataBlock = { [weak self] (_,data,_) in
            
            if let data = data {
                
                self!.tableView.updateData([data as! CMDBUserInfoBean])
                
                self!.emptyView.hidden = true
                
                self!.tableView.hidden = false
            }
        }
    }
    
    @objc private func itemClick(argu: TSArguInfo) {
        
        let user = argu.data as! CMDBUserInfoBean
        
        let otherTitles = user.iscommit ? ["个人主页","修改信息","删除"] : ["邀请","修改信息","删除"]
        
        let actionSheet = ACActionSheet(title: "", cancelButtonTitle: "取消", destructiveButtonTitle: "", otherButtonRedTitles: otherTitles) { [weak self] (idx) in
            
            if let strongSelf = self {
                
                if idx == 0 {
                    
                    if user.iscommit {
                        
                        let userVC = CMDBUserCenterViewController()
                        
                        userVC.title = "\(user.name)的个人主页"
                        
                        strongSelf.navigationController!.pushViewController(userVC, animated: true)
                        
                    } else {
                        let invite = CMDBInviteViewController()
                        
                        invite.title = "邀请"
                        
                        invite.user = user
                        
                        strongSelf.navigationController!.pushViewController(invite, animated: true)
                        
                        invite.dataBlock = { (_,_,_) in
                            
                            user.iscommit = true
                            
                            strongSelf.tableView.updateData(user, for: NSIndexPath(forRow: 0, inSection: 0))
                        }
                        
                    }
                    
                } else if idx == 1 {
                    
                    let modifyUserVC = CMDBModifyUserManageViewController()
                    
                    modifyUserVC.title = "二级用户信息修改"
                    
                    modifyUserVC.editAble = false
                    
                    strongSelf.navigationController!.pushViewController(modifyUserVC, animated: true)
                    
                } else if idx == 2 {
                    
                    
                    
                    
                }
            }
        }
        
        actionSheet.show()
    }
}